package com.anysoft.controller;

import com.anysoft.exception.MyException;
import com.anysoft.exception.MyExceptionHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;

@Controller
public class BaseController {
//    @ModelAttribute
//    public void isLogin(HttpSession session) throws Exception {
//        if (session.getAttribute("user") == null) {
//            throw new Exception("没有登录权限");
//        }
//    }

    // 基于@ExceptionHandler异常处理
    @ExceptionHandler
    public String exception(HttpServletRequest request, Exception ex) {
        request.setAttribute("ex", ex);
        // 根据不同错误转向不同页面，即异常与view的对应关系
        if (ex instanceof SQLException) {
            return "sql-error";
        } else if (ex instanceof MyException) {
            return "my-error";
        } else {
            return "error";
        }
    }
}
